

const state = {
  tabsView: [],
};

const mutations = {
  init_Tab(state, tab) {
    if(localStorage.getItem('tabsView')){
      state.tabsView = JSON.parse(localStorage.getItem('tabsView'));
    } else {
      state.tabsView = [{name:"index",path:"/index",title:"主页",affix:false}];
    }
  },
  add_Tab(state, tab) {
    //如果tabsview中已经存在该tab,则不添加
    if (state.tabsView.some(item => item.path === tab.path)) {
      return;
    } else {
      //如果tabsview中不存在该tab,则添加
      state.tabsView.push(tab);
      //并保存一份到localstorage中
      localStorage.setItem('tabsView', JSON.stringify(state.tabsView));
    }
    
  },
  remove_Tab(state, tab) {
    //如果tabsview中存在该tab,则删除
    if (state.tabsView.some(item => item.path === tab.path)) {
      state.tabsView = state.tabsView.filter(item => item.path !== tab.path);
      //并保存一份到localstorage中
      localStorage.setItem('tabsView', JSON.stringify(state.tabsView));
    } 
  },
};

const actions = {
  // ...existing code...
};

const getters = {
  getTabs: state => state.tabsView,
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};
